#include "Action.h"
#include "utility.h"
//(*InternalHeaders(Action)
#include <wx/intl.h>
#include <wx/string.h>
#include <wx/dcmemory.h>
#include <wx/dcclient.h>
//*)

//(*IdInit(Action)
const long Action::ID_BUTTON1 = wxNewId();
const long Action::ID_BUTTON2 = wxNewId();
const long Action::ID_BUTTON_DEL = wxNewId();
const long Action::ID_BUTTON_DEL_FRAME = wxNewId();
//*)
wxBEGIN_EVENT_TABLE(Action, wxPanel)
        EVT_TIMER(-1, Action::OnTimer)
wxEND_EVENT_TABLE()

Action::Action(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
	Create(parent, id, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("id"));
	Connect(wxEVT_PAINT,(wxObjectEventFunction)&Action::OnPaint);
	Connect(wxEVT_ERASE_BACKGROUND,(wxObjectEventFunction)&Action::OnBackground);
}

Action::~Action()
{
	//(*Destroy(Action)
	//*)
}

void Action::OnTimer(wxTimerEvent &event)
{
	Refresh();
}

void Action::Start()
{
	m_isRun = true;
	m_timer.SetOwner(this);
	if(m_actionInfo.type == EATSimple)
	{
		m_timer.Start(m_actionInfo.simple.frameSpace);
	}
	else
	{
		m_timer.Start(m_actionInfo.complex.frameSpace);
	}
}
void Action::Stop()
{
	m_isRun = false;
	m_timer.Stop();
}

void Action::Draw(wxDC &dc)
{
	wxSize size = GetClientSize();
	dc.SetBrush(*wxWHITE_BRUSH);
	dc.DrawRectangle(size);
	if((m_actionInfo.type == EATSimple) && (m_actionInfo.simple.imageName.Len() > 0))
	{
		if(m_isRun)
		{
			wxSize size = GetImageSize(m_actionInfo.simple.imageName);
			if(m_curDrawFrame >= (size_t)m_actionInfo.simple.frameNum)
				m_curDrawFrame = 0;
			wxRect rect;
			rect.x = size.GetWidth()/m_actionInfo.simple.frameNum*m_curDrawFrame;
			rect.y = 0;
			rect.SetWidth(size.GetWidth()/m_actionInfo.simple.frameNum);
			rect.SetHeight(size.GetHeight());
			DrawImage(dc,m_actionInfo.simple.imageName,&rect,0,0);
			m_curDrawFrame++;
		}
		else
		{
			DrawImage(dc,m_actionInfo.simple.imageName,NULL,0,0);
		}
	}
	else if(m_actionInfo.type == EATComplex)
	{
		if(m_isRun)
		{
			if(m_curDrawFrame >= m_actionInfo.complex.layerList.size())
				m_curDrawFrame = 0;
			if(m_actionInfo.complex.layerList.size() > 0)
			{
				SLayer &layer = m_actionInfo.complex.layerList[m_curDrawFrame];
				DrawImage(dc,layer.image.name,&(layer.image.rect),layer.x,layer.y);
				m_curDrawFrame++;
			}
		}
		else
		{
			int size = m_actionInfo.complex.layerList.size();
			for(int i = 0; i < size; i++)
			{
				SLayer &layer = m_actionInfo.complex.layerList[i];
				if(layer.isShow)
					DrawImage(dc,layer.image.name,&(layer.image.rect),layer.x,layer.y);
			}
		}
	}
}

void Action::OnPaint(wxPaintEvent& event)
{
	wxPaintDC dc(this);

    wxSize clientSize = GetClientSize();
    wxBitmap bmp(clientSize.GetWidth(),clientSize.GetHeight());
    wxMemoryDC memDC(bmp);
	//memDC.DrawRectangle(clientSize);
    Draw(memDC);
    
    dc.Blit(0,0,clientSize.GetWidth(),clientSize.GetHeight(),&memDC,0,0);
}

void Action::OnBackground(wxEraseEvent &event)
{
}
